package service

import (
	"QHMedLink/model" // 替换为实际路径

	"gorm.io/gorm"
)

// 1. 创建记录
func CreateTransfer(db *gorm.DB, transfer *model.EquipmentTransfer) error {
	if err := db.Create(transfer).Error; err != nil {
		return err
	}
	return nil
}

// 2. 查询记录（按主键）
func GetTransferByID(db *gorm.DB, id uint) (*model.EquipmentTransfer, error) {
	var transfer model.EquipmentTransfer
	if err := db.First(&transfer, id).Error; err != nil {
		return nil, err
	}
	return &transfer, nil
}

// 3. 条件查询（按城市和设备状态）
func GetTransfersByCondition(db *gorm.DB, city, condition string) ([]model.EquipmentTransfer, error) {
	var transfers []model.EquipmentTransfer
	query := db.Where("city = ? AND equipment_condition = ?", city, condition)
	if err := query.Find(&transfers).Error; err != nil {
		return nil, err
	}
	return transfers, nil
}

// 4. 更新记录（修改价格和状态）
func UpdateTransfer(db *gorm.DB, id uint, newPrice float64, newStatus string) error {
	return db.Model(&model.EquipmentTransfer{}).
		Where("equipment_id = ?", id).
		Updates(map[string]interface{}{
			"price":                newPrice,
			"certification_status": newStatus,
		}).Error
}

// 5. 删除记录
func DeleteTransfer(db *gorm.DB, id uint) error {
	return db.Where("equipment_id = ?", id).Delete(&model.EquipmentTransfer{}).Error
}
